//
//  SettingViewController.m
//  huashiedu
//
//  Created by oceans on 2018/8/30.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"
#import "ChangePhoneFirstViewController.h"
#import "LoginViewController.h"
#import "AboutUSViewController.h"
#import <SDWebImage/SDImageCache.h>
#import <MBProgressHUD.h>
#import "TipManager.h"

@interface SettingViewController () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) NSArray *titles;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.titles = @[@"手机号绑定", @"密码", @"关于", @"清除缓存"];
    [self setupViews];
}

- (void)setupViews {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"设置";
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight - XNavigationHeight) style:UITableViewStyleGrouped];
    _tableview.backgroundColor = COLOR(245, 245, 245, 1);
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorInset = UIEdgeInsetsMake(0, 20, 0, 0);
    [self.view addSubview:_tableview];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 9;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        static NSString *identity = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identity];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    
    static NSString *identity = @"SettingTableViewCell";
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
    if (cell == nil) {
        cell = [[SettingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identity];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        cell.textLabel.text = _titles[indexPath.row];
        cell.detailTextLabel.text = nil;
        if (indexPath.row == 0) {
            if ([UserModel shared].hasLogin) {
                cell.detailTextLabel.text = [UserModel shared].loginUser.mobile;
            }
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        
        if ([UserModel shared].hasLogin) {
            [self showLogoutAlert];
        }else {
            [self toLogin];
        }
    }else{
        
        if (indexPath.row == 0) {
            
            if ([UserModel shared].hasLogin) {
                ChangePhoneFirstViewController *con = [[ChangePhoneFirstViewController alloc] init];
                con.type = 0;
                [self.navigationController pushViewController:con animated:YES];
                [con getCode];
            }else {
                [self toLogin];
            }
        }else if (indexPath.row == 1) {
            if ([UserModel shared].hasLogin) {
                ChangePhoneFirstViewController *con = [[ChangePhoneFirstViewController alloc] init];
                con.type = 1;
                [self.navigationController pushViewController:con animated:YES];
                [con getCode];
            }else {
                [self toLogin];
            }
        }else if (indexPath.row == 2) {
            AboutUSViewController *con = [[AboutUSViewController alloc] init];
            [self.navigationController pushViewController:con animated:YES];
        }else {
            
            [MBProgressHUD showHUDAddedTo:self.view animated:NO];
            __weak typeof(self) weakself = self;
            [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                [MBProgressHUD hideHUDForView:weakself.view animated:NO];
                [TipManager showTip:@"清除成功"];
            }];
        }
    }
}

- (void)toLogin {
    UINavigationController *controller = [LoginViewController loginViewController];
    [self.navigationController presentViewController:controller animated:YES completion:nil];
}

- (void)showLogoutAlert {
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否退出当前账户" preferredStyle:UIAlertControllerStyleAlert];
    [controller addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [controller dismissViewControllerAnimated:YES completion:nil];
    }]];
    __weak typeof(self) weakself = self;
    [controller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [controller dismissViewControllerAnimated:YES completion:nil];
        [weakself logout];
    }]];
    [self presentViewController:controller animated:YES completion:nil];
}

- (void)logout {
    __weak typeof(self) weakself = self;
    [[UserModel shared] logoutSuccessBlock:^{
        [[UserModel shared] clearWhenLogout];
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
